<%--
  Created by IntelliJ IDEA.
  User: leixiaolong
  Date: 2018/12/4
  Time: 2:08 AM
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>菜单编辑管理</title>
    <link rel="stylesheet" href="/static/css/rpt-condition.css" media="all" type="text/css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/plugins/layui/css/layui.css" media="all"
          type="text/css">

    <style>
        .layui-input-inline {
            min-width: 300px;
            max-width: 600px;
        }
    </style>
</head>
<body>
<div>
    <div class="layui-row layui-col-space10" style="margin: 10px; background-color: white">
        <div class="layui-container">
            <div class="layui-form" lay-filter="select-condition">
                <div class="layui-form-item layui-col-md12">
                    <label class="layui-form-label ">菜单名称：</label>
                    <div class="layui-input-inline">
                        <input type="text" id="menu_name" name="menuName" class="layui-input-inline dom-add-input"
                               lay-verify="required">
                    </div>
                </div>

                <div class="layui-form-item layui-col-md12">
                    <label class="layui-form-label ">菜单类型：</label>
                    <div class="layui-input-inline">
                        <select id="menu_type" name="menuType" class="layui-form-select" lay-verify="required">
                        </select>
                    </div>
                </div>

                <div class="layui-form-item layui-col-md12">
                    <label class="layui-form-label">菜单描述：</label>
                    <div class="layui-input-inline">
                        <input type="text" id="menu_desc" name="menuDesc" class="layui-input-inline dom-add-input">
                    </div>
                </div>

                <div class="layui-form-item layui-col-md12">
                    <label class="layui-form-label">菜单url：</label>
                    <div class="layui-input-inline">
                        <div class="layui-input-inline">
                            <input type="text" id="menu_url" name="menuUrl" class="layui-input-inline dom-add-input">
                        </div>
                    </div>
                </div>

                <div class="layui-form-item layui-col-md12">
                    <label class="layui-form-label">菜单序号：</label>
                    <div class="layui-input-inline">
                        <div class="layui-input-inline">
                            <input type="text" id="menu_no" name="menuNo" class="layui-input-inline dom-add-input" lay-verify="number">
                        </div>
                    </div>
                </div>

                <div class="layui-form-item layui-col-md12">
                    <label class="layui-form-label ">父菜单：</label>
                    <div class="layui-input-inline">
                        <select id="father_menu_id" name="fatherMenuId" class="layui-form-select" lay-verify="required">
                            <option value="" selected>请选择</option>
                        </select>
                    </div>
                </div>

                <div class="layui-form-item layui-col-md12">
                    <label class="layui-form-label ">状态：</label>
                    <div class="layui-input-inline">
                        <select id="stat" name="stat" class="layui-form-select" lay-verify="required">
                            <option value=0 selected>正常</option>
                            <option value=1>停用</option>
                        </select>
                    </div>
                </div>

                <div class="layui-btn-group">
                    <button class="layui-btn layui-btn-normal layui-btn-warm" lay-submit data-type="submit">提交</button>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>

<script language='javascript' src="${pageContext.request.contextPath}/plugins/layui/layui.js"></script>
<script language='javascript' src="${pageContext.request.contextPath}/static/js/component/form-select.js"></script>

<script>
    layui.use(["form", "jquery", "layer"], function(){
        var form = layui.form, layer = layui.layer, $ = layui.jquery;
        // action="/sys/addMenuInfo" method="POST"

        var actives = {
            submit: function (field) {
                $.ajax({
                    url: 'addMenuInfo',
                    type: 'post',
                    data: field,
                    async: false,
                    success: function(result){
                        layer.msg("交易成功,"+result.data);
                        var index = parent.layer.getFrameIndex(window.name);

                        parent.reloadMenu();
                        parent.layer.close(index);
                    },
                    error: function(result){
                        layer.msg("交易失败,"+JSON.stringify(result));
                    }
                });
            }
        };

        form.on('submit', function (data) {
            var field = data.field;
            actives[$(this).data("type")].call(this, field);
        });
    });


    async function initSelect() {
        var tmp1 = initCodeTableSelect('menu_type', '/base/getMenuTypeList', null);
        var tmp2 = initMenuList();

        await tmp1;
        await tmp2;
    }

    async function init(data) {
        layui.use(["form", "jquery"], function () {
            var form = layui.form, $ = layui.jquery;

            initSelect();

            console.log("执行 init");

            form.val('select-condition', {
                "fatherMenuId": data.fatherMenuId,
                "menuType": data.menuType
            });

            form.render('select');
        });
    }

    function initMenuList() {
        layui.use(["form", "jquery"], function () {
            var form = layui.form, $ = layui.jquery;

            $.ajax({
                url: 'getRootList',
                type: 'get',
                async: false,
                success: function (result) {
                    var selection = $('#father_menu_id');
                    selection.find("option").remove();

                    var alloption = '<option value=\"\" selected>请选择</option>';
                    selection.append(alloption);
                    result.forEach(function (currval, index, array) {
                        var option = '';
                        option = '<option value=' + currval.id + '>' + currval.title + '</option>';
                        selection.append(option);
                    });
                    form.render('select');
                    console.log("渲染父菜单数据");
                },
                error: function (data) {
                    layer.msg("初始化select失败, select id:" + id.toString());
                }
            });
            console.log("ajax 父菜单渲染执行over");
        });
    }
</script>